if(!window.luren){
    window.luren = {};
}
luren.ui = {};



//处理图片切割的类
luren.ui.ImageCut = function (opts) {
    var me = this;
    this.wrapper_ = opts.wrapper;
    this.cutbox_ = this.wrapper_.find(".imagecut-box");

    //与切割窗口相关的数据
    this.size_ = {
        winwidth : opts.winwidth,  //窗口宽度
        winheight : opts.winheight,  //窗口高度
        cwwidth : opts.cutwidth,  //剪切图片的宽度
        cwheight : opts.cutheight,  //剪切图片的高度
        sidewidth : (opts.winwidth - opts.cutwidth) / 2,  //两边的宽度
        sideheight : (opts.winheight - opts.cutheight) / 2//上下两边的高度
    }

    //初始化切割窗口
    this._initCutBox();
    

    this.img_ = this.wrapper_.find(".imagecut-box > .imagecut-img").hide();
    this.img_.draggable();

    //图片大小数据
    this.imgsize_ = {
        width : 0,
        height : 0,
        real_width : 0,  	//缩放后的宽度
        real_height : 0	//缩放后的高度
    }

    //图片剪切数据
    this.imagedata_ = {
        x1 : 0,
        y1 : 0,
        x2 : 0,
        y2 : 0,
        zoom : 1,
        name : ''
    }

    //遮罩层opacity
    this.bgopacity_ = this.wrapper_.find(".imagecut-bgopacity").slider({
        min:0,
        max:1,
        step:0.01,
        value:0.5
    }).bind("slide", function(event, ui) {
        var value = me.bgopacity_.slider("value");
        me.setOpacity(value);
    });
    //图片放大缩小
    this.resizer_ = this.wrapper_.find(".imagecut-resizer").slider({
        min : 10,
        max : 100,
        step : 1,
        value : 100
    });

    //图片放大缩小事件
    this.resizer_.bind("slidechange", function(event, ui) {
        var zoom = me.resizer_.slider("value") / 100;
        me.zoomImg(zoom);
    });
    //滚轮事件
    this.cutbox_.mousewheel(function(objEvent, intDelta) {
        if(intDelta > 0) {
            //alert("Up");
            me.resizer_.slider('value',me.resizer_.slider('value')+5).slider('slide');
        } else if(intDelta < 0) {
            //alert("down");
            me.resizer_.slider('value',me.resizer_.slider('value')-5).slider('slide');
        }
    });
}

luren.ui.ImageCut.prototype = {
    /**
     *  初始化切割窗口
     */
    _initCutBox:function(){
        //调整宽高设置
        this.cutbox_.height(this.size_.winheight).width(this.size_.winwidth);
        this.cutbox_.find("> .imagecut-box-l").width(this.size_.sidewidth);
        this.cutbox_.find("> .imagecut-box-r").width(this.size_.sidewidth);

        this.cutbox_.find("> .imagecut-box-t")
        .height(this.size_.sideheight)
        .width(this.size_.cwwidth)
        .css({
            left : this.size_.sidewidth
        });

        this.cutbox_.find("> .imagecut-box-b")
        .height(this.size_.sideheight)
        .width(this.size_.cwwidth)
        .css({
            left : this.size_.sidewidth
        });
    },
    
    /**
     *  设置遮罩层透明度
     */
    setOpacity:function(opacity){
        this.cutbox_.find("> .imagecut-box-l,> .imagecut-box-r,> .imagecut-box-t,> .imagecut-box-b").css({
            opacity : opacity
        });
    },
    /**
     *  设置遮罩层颜色
     */
    setColor:function(color){
        this.cutbox_.find("> .imagecut-box-l,> .imagecut-box-r,> .imagecut-box-t,> .imagecut-box-b").css({
            backgroundColor:color
        });
    },
    
    
    /**
     * 设置将切割的图片,url暂时只能为本站的url
     */
    setPic : function(data) {
        var me = this;
        var image = new Image();
        image.onload = function() {
            if(image.width < me.size_.cwwidth || image.height < me.size_.cwheight) {
                alert("上传图片尺寸过小!!!");
                return;
            }

            me.img_.width(image.width);
            me.img_.height(image.height);

            me.img_[0].src = image.src;
            me.img_.show();
            me.initImgSize();
            me.updateSlider(image.width,image.height);
            me.centerImg();
            me.updateContainment();
        }
        image.onerror = function() {
            alert("该图片无法访问！");
        }
        image.src = data.url;
        this.imagedata_.name = data.name;
    },
    
    /**
     * 初始化图片
     */
    initImgSize : function() {
        this.imgsize_.real_width = this.imgsize_.width = this.img_.width();
        this.imgsize_.real_height = this.imgsize_.height = this.img_.height();
        this.imagedata_.x1 = 0
        this.imagedata_.y1 = 0
        this.imagedata_.x2 = 0
        this.imagedata_.y2 = 0
        this.imagedata_.zoom = 1
    },
    /**
     * 根据图片大小，调整slider的值
     */
    updateSlider : function(width,height) {
        var min = Math.max(this.size_.cwwidth / width , this.size_.cwheight / height);
        this.resizer_.slider("option","min", min * 100).slider("option","value",100);
    },
    
    
    /**
     * 获取法送到服务器端脚本的数据
     */
    getData : function() {
        var offset = this.img_.position();
        this.imagedata_.x1 = this.size_.sidewidth - offset.left;
        this.imagedata_.y1 = this.size_.sideheight - offset.top;

        this.imagedata_.x2 = this.imagedata_.x1 + this.size_.cwwidth;
        this.imagedata_.y2 = this.imagedata_.y1 + this.size_.cwheight;

        return this.imagedata_;
    },
    
    /**
     * 将图片居中
     */
    centerImg : function() {
        this.img_.css({
            top:(this.size_.winheight - this.imgsize_.real_height)/2,
            left:(this.size_.winwidth - this.imgsize_.real_width)/2
        });
    },
    /**
     *  放大缩小图片
     *  zoom 比例值
     */
    zoomImg:function(zoom){
        var me = this;
        me.imagedata_.zoom = zoom;

        //更新图片大小
        me.imgsize_.real_width = me.imgsize_.width * zoom;
        me.imgsize_.real_height = me.imgsize_.height * zoom;
        me.img_.width(me.imgsize_.real_width).height(me.imgsize_.real_height);

        me.updateContainment();
        me.centerImg();
    },
    
    /**
     * 更新拖到区域的范围
     */
    updateContainment : function() {
        var offset = this.cutbox_.offset();
        var s_width = this.size_.sidewidth + this.size_.cwwidth;
        var s_height = this.size_.sideheight + this.size_.cwheight;

        this.img_.draggable('option', 'containment', [offset.left - this.imgsize_.real_width + s_width, offset.top - this.imgsize_.real_height + s_height, offset.left + this.size_.sidewidth, offset.top + this.size_.sideheight]);
    }
}




var _SUCCESS = 1;

/**
 * 文件选择框
 */
function AjaxFileInput(opts) {
    this.form_ = opts.form;
    this.file_ = opts.file;
    this.filea_ = opts.filea;

    //事件
    this.onsubmit_ = opts.onsubmit;
    this.onsuccess_ = opts.onsuccess;
    this.onerror_ = opts.onerror;

    var me = this;
    //file随鼠标移到
    this.filea_.mousemove(function(event) {
        var offest = me.filea_.offset();
        var left = event.clientX - offest.left;
        var top = event.clientY - offest.top;
        if(left < 0 || top < 0 || left > 145 || top > 30) {
            me.file_.hide();
            return;
        } else {
            me.file_.show().css({
                left : left - 30,
                top : top - 15
            });
        }
    });
    this.form_.ajaxForm({
        dataType : 'json',
        resetForm : true,
        beforeSubmit : function() {
            if(me.onsubmit_) {
                return me.onsubmit_.call(me, me.file_);
            }
            return true;
        },
        success : function(data) {
            if(data) {
                if(data.status == _SUCCESS) {
                    if(me.onsuccess_) {
                        me.onsuccess_.call(me, data);
                    }
                } else {
                    if(me.onerror_) {
                        me.onerror_.call(me, data);
                    } else {
                        alert(data.msg);
                    }
                }
            }
        }
    });

    this.file_.change(function() {
        me.form_.trigger('submit');
    });
}

AjaxFileInput.prototype.setOnSubmit = function(fun) {
    this.onsubmit_ = fun;
}
AjaxFileInput.prototype.setOnSuccess = function(fun) {
    this.onsuccess_ = fun;
}
AjaxFileInput.prototype.setOnError = function(fun) {
    this.onerror_ = fun;
}